﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UsbBitWacker;

namespace TestApp
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
        }

        internal UBW ubw;

        protected void Log(string text)
        {
            uxLog.SelectionStart = uxLog.TextLength;
            uxLog.AppendText(text + "\r\n");
            uxLog.ScrollToCaret();
        }

        protected int PortAVal { get { return int.Parse(uxPortAVal.Text); } }
        protected int PortBVal { get { return int.Parse(uxPortBVal.Text); } }
        protected int PortCVal { get { return int.Parse(uxPortCVal.Text); } }
        protected string PortSel { get { return uxPort.Text; } }
        protected int PinSel { get { return int.Parse(uxPin.Text); } }

        private void ucOpen_Click(object sender, EventArgs e)
        {
            ubw = new UBW();
            Log(ubw.Open().ToString());
        }

        private void ucReset_Click(object sender, EventArgs e)
        {
            ubw.Reset();
        }

        private void ucClose_Click(object sender, EventArgs e)
        {
            Log(ubw.Close().ToString());
        }

        private void ucGetAnalogs_Click(object sender, EventArgs e)
        {
            Log(ubw.ReadAnalogInputsCSV());
        }

        private void ucConfigIO_Click(object sender, EventArgs e)
        {
            Log(ubw.ConfigureIO(PortAVal, PortBVal, PortCVal, int.Parse(uxAnalogCount.Text)).ToString());
        }

        private void ucInputAll_Click(object sender, EventArgs e)
        {
            Log(ubw.ReadAllInputsCSV());
        }

        private void ucOutputAll_Click(object sender, EventArgs e)
        {
            Log(ubw.WriteAllOutputs(PortAVal, PortBVal, PortCVal).ToString());
        }

        private void ucGetPinVal_Click(object sender, EventArgs e)
        {
            Log(ubw.ReadDigitalPin(PortSel, PinSel).ToString());
        }

        private void ucSetPinVal_Click(object sender, EventArgs e)
        {
            Log(ubw.WriteDigitalPin(PortSel, PinSel, 1).ToString());
        }

        private void ucResetPinVal_Click(object sender, EventArgs e)
        {
            Log(ubw.WriteDigitalPin(PortSel, PinSel, 0).ToString());
        }

        private void ucSetPinDir_Click(object sender, EventArgs e)
        {
            if (uxPinDirValue.SelectedIndex == 0)
                Log(ubw.ConfigurePinAsInput(PortSel, PinSel).ToString());
            else
                Log(ubw.ConfigurePinAsOutput(PortSel, PinSel).ToString());
        }

        private void ucSetFreq_Click(object sender, EventArgs e)
        {
            ubw.SetFrequencyOutput(PortSel, PinSel, int.Parse(uxFreq.Text));
        }

    }
}
